// miniprogram/pre_loadModules/orderModule/pages/orderList/orderList.js
import { getOrderList, deleteOrder, customerReminder, confirmdeliveryDone, cancelOrderByCustomer, memberAfterCommens,combinedScoreByCustomor } from '@/miniprogram/pre_loadModules/orderModule/API/order'

var ordercount = getApp()
Page({

   /**
    * 页面的初始数据
    */
   data: {
      height: null, dialogHeight: null, orderList: [], orderStatus: 0, currentList: [], pagedata: { currentPage: 1, pageSize: 5, sort: 'desc', }, sendData: {}, deliverySn: '', combinedScore: 0, comment: '', skuScore: 5, packageScore: 5, deliverySpeed: 5, deliveryPerson: 5,
      deliverydialogShow: false, remindDialogShow: false, remindValue: '',subIndex:0,
      commentDialogShow: false,
      orderStatusDisplay: [
         { id: 1, color: 'orange', status: '待支付', actived:0,options: ['立即支付', '取消订单', '联系商家'] },
         { id: 2, color: 'skyblue', status: '待发货', actived:0,options: ['物流信息', '我要催单', '联系商家'] },
         { id: 3, color: 'green', status: '待收货', actived:0,options: ['物流信息', '确认收货', '我要催单', '联系物流', '联系商家'] },
         { id: 4, color: 'gray', status: '已完成', actived:0,options: ['写评价'] },
         { id: 5, color: 'red', status: '未评价', actived:0,options: ['申请售后','售后进度','联系商家'] }
      ],
      deliveryStep: [
         { id: 1, text: "标题", desc: "描述" },
         { id: 1, text: "标题2", desc: "描述2" },
         { id: 1, text: "2", desc: "" },
         { id: 1, text: "3", desc: "" },
         { id: 1, text: "2", desc: "" },
         { id: 1, text: "2", desc: "" },
         { id: 1, text: "3", desc: "" },
         { id: 1, text: "2", desc: "" },
         { id: 1, text: "2", desc: "" },
         { id: 1, text: "3", desc: "" },
         { id: 1, text: "2", desc: "" },
         { id: 1, text: "2", desc: "" },
         { id: 1, text: "3", desc: "" },
         { id: 1, text: "2", desc: "" },
         { id: 1, text: "2", desc: "" },
         { id: 1, text: "3", desc: "" },
         { id: 1, text: "2", desc: "" },
         { id: 1, text: "3", desc: "" }
      ],
      topNavSteps: [
         { id: 0, text: '待支付', actived: 0 },
         { id: 1, text: '待发货', actived: 0 },
         { id: 2, text: '待收货', actived: 0 },
         { id: 3, text: '已完成', actived: 0 },
         { id: 4, text: '全部/售后', actived: 0 }
      ]
   },
   bindwritecommentConfirm(e) {
      let { skuid, ordersn } = e.currentTarget.dataset
      let { combinedScore, comment, skuScore, packageScore, deliverySpeed, deliveryPerson, } = this.data
      let sendData = {}
      sendData.combinedScore = combinedScore, sendData.comment = comment, sendData.skuScore = skuScore, sendData.packageScore = packageScore, sendData.skuId = skuid, sendData.orderSn = ordersn, sendData.deliverySpeed = deliverySpeed, sendData.deliveryPerson = deliveryPerson
      this.memberAfterCommensFun(sendData)
   },
   commentDialogCancel() {
      this.setData({
         commentDialogShow: false,
         combinedScore: 0, comment: '', skuScore: 5, packageScore: 5, deliverySpeed: 5, deliveryPerson: 5,
      })
   },

   /**
    * 
    * @param {文字评价} e 
    */
   writecommentBtn(e) {
      this.setData({
         commentDialogShow: true
      })
   },

   /**
    * 
    * @param {综合星级评价} e 
    */
   RateonChange(e) {
      let { ordersn } = e.currentTarget.dataset
      let { detail, } = e
      let sendData = {}
      sendData.orderSn = ordersn
      sendData.combinedScore = detail
      this.setData({
         combinedScore:detail
      })
      this.combinedScoreByCustomorFun(sendData)
   },

   async combinedScoreByCustomorFun(data){
      let res = await combinedScoreByCustomor(data)
      if (res.data.code === 200) {
         wx.showToast({
           title: '评价成功',
         })
      }
   },
   async memberAfterCommensFun(data) {
      let { comments } = this.data
      let res = await memberAfterCommens(data)
      if (res.data.code == 200) {
         wx.showToast({
            title: '评价已提交！',
         })
         comments = {},
            this.setData({
               commentDialogShow: false,
               comments,
               commentDialogShow: false
            })
      }
   },

   bindremindCancel(e) {
      this.setData({
         remindDialogShow: false
      })
   },
   bindstepNavbar(e) {
      let { orderStatus, pagedata } = this.data
      let { detail } = e
      orderStatus = detail
      this.setData({
         orderStatus
      })
      this.getOrderListFun(pagedata)
   },

   /**
    * 
    * @param {加急弹窗确认}} e 
    */
   bindReminderDialogConfirm(e) {
      let { sendData, remindValue, orderSn, currentList } = this.data
      let t = { ...sendData }
      t.remindValue = remindValue
      this.setData({
         remindDialogShow: false
      })
      this.reminderFun(t)
   },
   /**
    * 生命周期函数--监听页面加载
    */
   onLoad(options) {
      let { orderStatus } = options
      orderStatus = parseInt(orderStatus)
      this.setData({
         orderStatus
      })
   },
   scollrun() {
   },

   bindopsBtn(e) {
      let { opstype, orderstatus, ordersn, deliverysn } = e.currentTarget.dataset
      let { currentList, deliverydialogShow, sendData } = this.data
      console.log(e);
      switch (orderstatus) {
         case 0:
            switch (opstype) {
               case 0:
                  wx.redirectTo({
                     url: `/miniprogram/pre_loadModules/orderModule/pages/orderPayment/orderPayment?orderSn=${ordersn}`,
                  })
                  return;
               case 1:
                  wx.showModal({
                     title: "确定删除这条订单吗？",
                     success(res) {
                        if (res.confirm) {
                           let data = {}
                           data.orderSn = ordersn
                           this.deleteOrderFun(data)
                        }
                        if (res.cancel) {
                           wx.showToast({
                              title: '已取消',
                              icon: 'error'
                           })
                        }
                     }
                  })
                  return;
               case 2:
                  wx.redirectTo({
                     url: '/contactUsModule/pages/index',
                  })
                  return;
            }
         case 1:
            switch (opstype) {
               case 0:
                  this.setData({
                     deliverydialogShow: true,
                     deliverySn: deliverysn
                  })
                  return;
               case 1:
                  let sendData = {}
                  let od = currentList
                  console.log("加急按照你",ordersn);
                  sendData.orderSn = ordersn
                  od.forEach(item => {
                     if (item.orderSn === ordersn) {
                        if (item.reminder === 1) {
                           wx.showToast({
                              title: '此订单已经加急',
                              icon: 'error'
                           })
                           return;
                        }
                        this.setData({
                           sendData,
                           remindDialogShow: true
                        })
                     }
                  })
                  return;
               case 2:
                  wx.navigateTo({
                     url: '/contactUsModule/pages/index',
                  })
            }
         case 2:
            switch (opstype) {
               case 0:
                  this.setData({
                     deliverydialogShow: true,
                     deliverySn: deliverysn
                  })
                  return;
               case 1:
                  wx.showModal({
                     title: '您确定收货吗？',
                     complete: (res) => {
                        if (res.cancel) {
                           wx.showToast({
                              title: '已取消操作！',
                              icon: 'error'
                           })
                        }
                        if (res.confirm) {
                           let s = {}
                           s.orderSn = ordersn
                           this.confirmdeliveryDoneFun(s)
                        }
                     }
                  })
                  return;
               case 2:
                  let sendData = {}
                  let od = currentList
                  sendData.orderSn = ordersn
                  od.forEach(item => {
                     if (item.orderSn === ordersn) {
                        if (item.reminder === 1) {
                           wx.showToast({
                              title: '此订单已经加急',
                              icon: 'error'
                           })
                           return;
                        }
                        this.setData({
                           sendData,
                           remindDialogShow: true
                        })
                     }
                  })
                  return;
               case 3:
                  wx.redirectTo({
                     url: '/contactUsModule/pages/contactDelivery/contactDelivery',
                  })
                  return;

               case 4:
                  wx.redirectTo({
                     url: '/contactUsModule/pages/index',
                  })
                  return;

            }
         case 3:

      }
   },
   /**
    * 
    * @param {确认收货} data 
    */
   async confirmdeliveryDoneFun(data) {
      let { pagedata } = this.data
      let res = await confirmdeliveryDone(data)
      if (res.data.code === 200) {

         this.getOrderListFun(pagedata)
      }
   },

   /**
    * 
    * @param {取消订单} data 
    */
   async cancelOrderByCustomerFun(data) {
      let res = await cancelOrderByCustomer(data)
   },

   /**
    * 
    * @param {物流窗口} e 
    */
   bindDialogConfirm(e) {
      let { deliverydialogShow } = this.data
      deliverydialogShow = false
      this.setData({
         deliverydialogShow
      })
   },
   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady() {

   },

   /**
    * 
    * @param {催单} data 
    */
   async reminderFun(data) {
      let {pagedata} = this.data
      let res = await customerReminder(data)
      if (res.data.code === 200) {
         wx.showToast({
            title: '催单成功！',
         })
      }
      this.getOrderListFun(pagedata)
   },
   /**
    * 生命周期函数--监听页面显示
    */
   onShow() {
      let { height, pagedata, orderStatus, dialogHeight, topNavSteps } = this.data
      height = wx.getWindowInfo().windowHeight
      let temp = height;
      height = (temp - 100) + 'px'
      dialogHeight = (temp - 400) + 'px'
      this.getOrderListFun(pagedata)
      topNavSteps = topNavSteps.map(item => {
         if (item.id === orderStatus ) {
            item.actived = parseInt(item.id)
         }
         return item
      })
      this.setData({
         height,
         topNavSteps,
         dialogHeight
      })
      switch (orderStatus) {
         case 0:
            wx.setNavigationBarTitle({
               title: '待付款订单',
            })
            return;
         case 1:
            wx.setNavigationBarTitle({
               title: '待发货订单',
            })
            return;
         case 2:
            wx.setNavigationBarTitle({
               title: '待收货订单',
            })
            return;
         case 3:
            wx.setNavigationBarTitle({
               title: '待评价订单',
            })
            return;
         case 4:
            wx.setNavigationBarTitle({
               title: '售后/退款',
            })
            return;
         case 5:
            wx.setNavigationBarTitle({
               title: '售后服务',
            })
      }


   },

   /**
    * 
    * @param {订单列表} data 
    */
   async getOrderListFun(data) {
      let { orderList, orderStatus, currentList, } = this.data
      let res = await getOrderList(data)
      if (res.data.code == 200) {
         orderList = res.data.data
         currentList = orderList.filter(item => {
            if (item.status === orderStatus) {
               item.orderItems.forEach(element => {
                  element.skuAttrValues = element.skuAttrValues.split(';')
               });
               return item
            }
         })
         let countList = [{id:0,count:0},{id:1,count:0},{id:2,count:0},{id:3,count:0},{id:4,count:0},]
         for (let i = 0; i < orderList.length; i++) {
            if (orderList[i].status === 0) {
               countList[0].count = countList[0].count + 1
            }
            if (orderList[i].status === 1) {
               countList[1].count = countList[1].count + 1
            }
            if (orderList[i].status === 2) {
               countList[2].count = countList[2].count + 1
            }
            if (orderList[i].status === 3) {
               countList[3].count = countList[3].count + 1
            }
            if (orderList[i].status === 4) {
               countList[4].count = countList[4].count + 1
            }
         }
         ordercount.globalData.orderCount = countList
         this.setData({
            orderList,
            currentList
         })
      }
   },

   /**
    * 
    * @param {删除订单} data 
    */
   async deleteOrderFun(data) {
      let { pagedata } = this.data
      let res = await deleteOrder(data)
      if (res.data.code == 200) {
         wx.showToast({
            title: '删除成功！',
         })
      }
      this.getOrderListFun(pagedata)
   },

   /**
    * 
    * @param {获取物流信息} data 
    */
   async getdeliveryInfo(data) {
      let res = await this.getdeliveryInfo(data)
      if (res.data.code === 200) {

      }
   },
   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide() {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload() {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh() {

   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom() {

   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage() {

   }
})